package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.MatchDao;
import cz.khfotbal.domain.Delegat;
import cz.khfotbal.domain.Match;
import cz.khfotbal.domain.MatchDelegat;
import cz.khfotbal.domain.MatchReferee;
import cz.khfotbal.domain.Referee;
import cz.khfotbal.domain.Season;
import cz.khfotbal.domain.SeasonCompetionTeam;
import cz.khfotbal.domain.SeasonCompetionTeamMatch;
import cz.khfotbal.domain.Team;

@Repository
@Transactional
public class MatchDaoImpl implements MatchDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;

	public List<Match> findAllMatchesByHomeTeam(Team team) {
		session = sessionFactory.getCurrentSession();
		List<Match> list = session.createQuery(
				"from SeasonCompetion" + team.getId()).list();
		return list;
	}

	public void saveMatch(Match match) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(match);
	}

	public void saveMatchDelegat(MatchDelegat md) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(md);

	}

	public MatchDelegat getMatchDelegat(Match match, Delegat delegat) {
		session = sessionFactory.getCurrentSession();
		return (MatchDelegat) session.createQuery(
				"from MatchDelegat md where md.match.id=" + match.getId()
						+ " and md.delegat.id=" + delegat.getId())
				.uniqueResult();
	}

	public List<MatchDelegat> getMatchDelegatByMatch(Match match) {
		session = sessionFactory.getCurrentSession();
		List<MatchDelegat> list = session.createQuery(
				"from MatchDelegat md where md.match.id=" + match.getId())
				.list();
		return list;
	}
	
	public Match findMatchById(int id){
		session = sessionFactory.getCurrentSession();
		Match result = (Match) session.createQuery(
				"from Match m where m.id=" + id)
				.uniqueResult();
		return result;
	}

	public void deleteReferees(Match match) {
		session = sessionFactory.getCurrentSession();
		// match.getMatchReferees().clear();
		session.save(match);
		// List<MatchReferee> rList = session.createQuery(
		// "from MatchReferee mr where mr.match.id=" + match.getId())
		// .list();
		// for (MatchReferee mr : rList) {
		// session.delete(mr);
		// }

	}

	public void setReferee(Referee referee, Match match) {
		session = sessionFactory.getCurrentSession();
		MatchReferee mr = new MatchReferee();
		mr.setReferee(referee);
		mr.setMatch(match);
		mr.setTyp(referee.getTyp());
		session.save(mr);
	}

	public void deleteMatch(Match selectedMatch) {
		session = sessionFactory.getCurrentSession();
		session.delete(selectedMatch);

	}

	public List<Match> findMatchesBySeasonAndTeam(Season season, Team team) {
		session = sessionFactory.getCurrentSession();
		SeasonCompetionTeam sct = (SeasonCompetionTeam) session.createQuery(
				"from SeasonCompetionTeam sct where sct.seasonCompetion.season.id="
						+ season.getId() + " and sct.team.id=" + team.getId())
				.uniqueResult();
		List<SeasonCompetionTeamMatch> list = session.createQuery(
				"from SeasonCompetionTeamMatch sctm where sc.seasonCompetionTeam.id"
						+ sct.getId()).list();
		return null;
	}

	public List<Match> findMatchesByDate(String date) {
		session = sessionFactory.getCurrentSession();
		List<Match> list = session.createQuery(
				"from Match where datum = '" + date + "'").list();
		return list;
	}
	
}
